﻿Imports System.Xml

Public Class PMTParametresGeneraux
    Implements IPMTParametresGeneraux

#Region "Variables privées"
    Private _AnneeRealisation As Nullable(Of Integer)
    Private _MoisRealisation As Nullable(Of Integer)
    Private _AnneeDernierArrete As Nullable(Of Integer)
    Private _MoisDernierArrete As Nullable(Of Integer)
    Private _ComplementNom As String
    Private _NombreAnneesProjection As Integer
    Private _RacineInfocentre As IDirectoryPMT
    Private _RacineParametres As IDirectoryPMT
#End Region

#Region "Constructeur"
    Public Sub New()
        _RacineInfocentre = New DirectoryPMT_NonRenseigne
        _RacineParametres = New DirectoryPMT_NonRenseigne
        _NombreAnneesProjection = 5
    End Sub
#End Region

#Region "Propriétés"

    Public Property AnneeRealisation As Nullable(Of Integer) Implements IPMTParametresGeneraux.AnneeRealisation
        Get
            Return _AnneeRealisation
        End Get
        Set(value As Nullable(Of Integer))
            _AnneeRealisation = value
            RaiseEvent AnneeRealisation_Changed(Me, New EventArgs)
        End Set
    End Property
    Public Property MoisRealisation As Nullable(Of Integer) Implements IPMTParametresGeneraux.MoisRealisation
        Get
            Return _MoisRealisation
        End Get
        Set(value As Nullable(Of Integer))
            _MoisRealisation = value
            RaiseEvent MoisRealisation_Changed(Me, New EventArgs)
        End Set
    End Property

    Public Property AnneeDernierArrete As Nullable(Of Integer) Implements IPMTParametresGeneraux.AnneeDernierArrete
        Get
            Return _AnneeDernierArrete
        End Get
        Set(value As Nullable(Of Integer))
            _AnneeDernierArrete = value
            RaiseEvent AnneeDernierArrete_Changed(Me, New EventArgs)
        End Set
    End Property
    Public Property MoisDernierArrete As Nullable(Of Integer) Implements IPMTParametresGeneraux.MoisDernierArrete
        Get
            Return _MoisDernierArrete
        End Get
        Set(value As Nullable(Of Integer))
            _MoisDernierArrete = value
            RaiseEvent MoisDernierArrete_Changed(Me, New EventArgs)
        End Set
    End Property

    Public Property ComplementNom As String Implements IPMTParametresGeneraux.ComplementNom
        Get
            Return _ComplementNom
        End Get
        Set(value As String)
            _ComplementNom = value
        End Set
    End Property

    Public Property NombreAnneesProjection As Integer Implements IPMTParametresGeneraux.NombreAnneesProjection
        Get
            Return _NombreAnneesProjection
        End Get
        Set(value As Integer)
            _NombreAnneesProjection = value
            RaiseEvent NombreAnneesProjection_Changed(Me, New EventArgs)
        End Set
    End Property

    Public Property RacineInfocentre As IDirectoryPMT Implements IPMTParametresGeneraux.RacineInfocentre
        Get
            Return _RacineInfocentre
        End Get
        Set(value As IDirectoryPMT)
            If value IsNot _RacineInfocentre Then
                _RacineInfocentre = value
                RaiseEvent RacineInfocentre_Changed(Me, New EventArgs)
            End If
        End Set
    End Property
    Public Property RacineParametres As IDirectoryPMT Implements IPMTParametresGeneraux.RacineParametres
        Get
            Return _RacineParametres
        End Get
        Set(value As IDirectoryPMT)
            If _RacineParametres IsNot value Then
                _RacineParametres = value
                RaiseEvent RacineParametre_Changed(Me, New EventArgs)
            End If
        End Set
    End Property

    ''' <summary>
    ''' Mois et Année de réalisation au format AAAAMM. Renvoie "-" si l'un des deux n'est pas renseigné
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public ReadOnly Property Mois As String Implements IPMTParametresGeneraux.Mois
        Get
            Return AnneeMoisConverter.AnneeMoisToString_Simple(AnneeRealisation, MoisRealisation)
        End Get
    End Property

#End Region

#Region "Event"
    Public Event NombreAnneesProjection_Changed(sender As Object, e As EventArgs) Implements IPMTParametresGeneraux.NombreAnneesProjection_Changed
    Public Event RacineInfocentre_Changed(sender As Object, e As EventArgs) Implements IPMTParametresGeneraux.RacineInfocentre_Changed
    Public Event RacineParametre_Changed(sender As Object, e As EventArgs) Implements IPMTParametresGeneraux.RacineParametre_Changed
    Public Event AnneeDernierArrete_Changed(sender As Object, e As EventArgs) Implements IPMTParametresGeneraux.AnneeDernierArrete_Changed
    Public Event MoisDernierArrete_Changed(sender As Object, e As EventArgs) Implements IPMTParametresGeneraux.MoisDernierArrete_Changed
    Public Event AnneeRealisation_Changed(sender As Object, e As EventArgs) Implements IPMTParametresGeneraux.AnneeRealisation_Changed
    Public Event MoisRealisation_Changed(sender As Object, e As EventArgs) Implements IPMTParametresGeneraux.MoisRealisation_Changed
#End Region

#Region "Méthodes de serialization"
    Public Function GetSchema() As Xml.Schema.XmlSchema Implements Xml.Serialization.IXmlSerializable.GetSchema
        Return Nothing
    End Function

    Public Sub ReadXml(reader As Xml.XmlReader) Implements Xml.Serialization.IXmlSerializable.ReadXml

        Dim XmlParamGenSerialization As XElement = XElement.Load(reader)

        'Créer l'objet arboPMT correspondant
        'Dim test = XmlParamGenSerialization.Descendants.<AnneeRealisation>.FirstOrDefault().Value

        Me.AnneeRealisation = XmlParamGenSerialization.<PMTParametresGeneraux>.<AnneeRealisation>.FirstOrDefault.Value
        Me.MoisRealisation = XmlParamGenSerialization.<PMTParametresGeneraux>.<MoisRealisation>.FirstOrDefault.Value
        Me.AnneeDernierArrete = XmlParamGenSerialization.<PMTParametresGeneraux>.<AnneeDernierArrete>.FirstOrDefault.Value
        Me.MoisDernierArrete = XmlParamGenSerialization.<PMTParametresGeneraux>.<MoisDernierArrete>.FirstOrDefault.Value
        Me.NombreAnneesProjection = XmlParamGenSerialization.<PMTParametresGeneraux>.<NombreAnneesProjection>.FirstOrDefault.Value

        Dim XmlRacineInfo As XElement = XmlParamGenSerialization.<PMTParametresGeneraux>.<RacineInfocentre>.FirstOrDefault
        Dim XmlRacineParam As XElement = XmlParamGenSerialization.<PMTParametresGeneraux>.<RacineParametres>.FirstOrDefault

        Me.RacineParametres = DirectoryPMT_Builder.ReadXml(XmlRacineParam.CreateReader)
        Me.RacineInfocentre = DirectoryPMT_Builder.ReadXml(XmlRacineInfo.CreateReader)
        'Do
        '    If reader.Read() Then
        '        If reader.NodeType = XmlNodeType.Element Then
        '            If reader.Name = "AnneeRealisation" Then
        '                If (reader.Read()) Then
        '                    If (reader.NodeType = XmlNodeType.Text) Then Me.AnneeRealisation = Integer.Parse(reader.Value)
        '                End If
        '            ElseIf reader.Name = "MoisRealisation" Then
        '                If (reader.Read()) Then
        '                    If (reader.NodeType = XmlNodeType.Text) Then Me.MoisRealisation = Integer.Parse(reader.Value)
        '                End If
        '            ElseIf reader.Name = "AnneeDernierArrete" Then
        '                If (reader.Read()) Then
        '                    If (reader.NodeType = XmlNodeType.Text) Then Me.AnneeDernierArrete = Integer.Parse(reader.Value)
        '                End If
        '            ElseIf reader.Name = "MoisDernierArrete" Then
        '                If (reader.Read()) Then
        '                    If (reader.NodeType = XmlNodeType.Text) Then Me.MoisDernierArrete = Integer.Parse(reader.Value)
        '                End If
        '            End If
        '        End If
        '    End If
        'Loop Until reader.Name = "/ParametresGeneraux"
    End Sub

    Public Sub WriteXml(writer As Xml.XmlWriter) Implements Xml.Serialization.IXmlSerializable.WriteXml
        writer.WriteStartElement("PMTParametresGeneraux")

        writer.WriteElementString("AnneeRealisation", AnneeRealisation)
        writer.WriteElementString("MoisRealisation", MoisRealisation)
        writer.WriteElementString("AnneeDernierArrete", AnneeDernierArrete)
        writer.WriteElementString("MoisDernierArrete", MoisDernierArrete)
        writer.WriteElementString("ComplementNom", ComplementNom)
        writer.WriteElementString("NombreAnneesProjection", NombreAnneesProjection)
        writer.WriteStartElement("RacineInfocentre")
        RacineInfocentre.WriteXml(writer)
        writer.WriteEndElement()
        writer.WriteStartElement("RacineParametres")
        RacineParametres.WriteXml(writer)
        writer.WriteEndElement()

        writer.WriteEndElement()
    End Sub
#End Region

End Class